import {
    reqRegister,
    reqLogin,
    reqUpdateUser,
    reqUser,
    reqUserList,
    reqChatMsgList,
    reqReadMsg
} from '../api'
import {
    AUTH_SUCCESS,
    ERROR_MSG,
    RECEIVE_USER,
    RESET_USER,
    RECEIVE_USER_LIST,
    RECEIVE_MSG_LIST,
    RECEIVE_MSG,
    MSG_READ
} from './action-types'

import io from 'socket.io-client'

//
/*
*/
const authSuccess=(user)=>({type:AUTH_SUCCESS,data:user})
const errorMsg=(msg)=>({type:ERROR_MSG,data:msg})
const receiveUser=(user)=>({type:RECEIVE_USER,data:user})
export const resetUser=(msg)=>({type:RESET_USER,data:msg})
const receiveUserList=(userList)=>({type:RECEIVE_USER_LIST,data:userList})
const receiveMsgList=({users,chatMsgs,userid})=>({type:RECEIVE_MSG_LIST,data:{users,chatMsgs,userid}})
const receiveMsg=(chatMsg,userid) => ({type:RECEIVE_MSG,data:{chatMsg,userid}})
const msgRead=(count,from,to) => ({type:MSG_READ,data:{count,from,to}})

export const readMsg=(from,to)=>{
    return async dispatch=>{
        const response = await reqReadMsg(from)
        const result = response.data
        if(result.code === 0){
            const count =result.data
            dispatch(msgRead(count,from,to))
        }
    }
}

export const register=(user)=>{
    const {username,password,restpassword} = user
    if(!username){
        return errorMsg('用户名不能为空')
    }
    if(password!==restpassword){
        return errorMsg('两次密码要一致')
    }
    return async dispatch=>{
        const response = await reqRegister(user)
        const result = response.data
        if(result.code===0){
            getMsgList(dispatch,result.data._id)
            dispatch(authSuccess(result.data))
        } else{
            dispatch(errorMsg(result.msg))
        }
    }
}

export const login=(user)=>{
    const {username,password} = user
    if(!username){
        return errorMsg('用户名不能为空')
    }
    if(!password){
        return errorMsg('密码不能为空')
    }
    return async dispatch=>{
        const response = await reqLogin(user)
        const result = response.data
        if(result.code===0){
            getMsgList(dispatch,result.data._id)
            dispatch(authSuccess(result.data))
        } else{
            dispatch(errorMsg(result.msg))
        }
    }
}
/**
 * 更新用户
 */
export const updateUser=(user)=>{
    return async dispatch=>{
        const response = await reqUpdateUser(user)
        const result = response.data
        if(result.code===0){
            dispatch(receiveUser(result.data))
        } else{
            dispatch(resetUser(result.msg))
        }
    }
}

export const getUser = ()=>{
    return async dispatch=>{
        const response = await reqUser()
        const result = response.data
        if(result.code===0){
            getMsgList(dispatch,result.data._id)
            dispatch(receiveUser(result.data))
        } else{
            dispatch(resetUser(result.msg))
        }
    }
}

export const getUserList = (type)=>{
    return async dispatch=>{
        const response = await reqUserList(type)
        const result = response.data
        if(result.code===0){
            dispatch(receiveUserList(result.data))
        }
    }
}

/**
 * 单例对象
 * 1.创建前
 * 2。创建后
 */
//
async function getMsgList(dispatch,userid){
    initIo(dispatch,userid)
    const response= await reqChatMsgList()
    const result = response.data
        if(result.code===0){
            const {users,chatMsgs} = result.data
            dispatch(receiveMsgList(result.data,userid))
        }

}

function initIo(dispatch,userid){
    if(!io.socket){
        // 连接服务器, 得到与服务器的连接对象
    io.socket = io('ws://localhost:4000')
    // 绑定监听, 接收服务器发送的消息
    io.socket.on('receiveMsg', function (chatMsg) {
    if(userid===chatMsg.from || userid===chatMsg.to){
        dispatch(receiveMsg(chatMsg,userid))
    }
    })
    }
}

export const sendMsg = ({from,to,content})=>{
    return dispatch =>{
        console.log('sendMsg',{from,to,content})
        //
        io.socket.emit('sendMsg',{from,to,content})
    }
}

